//
//  DBManager.m
//  DataBase
//
//  Created by  江志磊 on 14-1-8.
//  Copyright (c) 2014年  江 志磊. All rights reserved.
//

#import "DBManager.h"
#import "FMDatabase.h"
@implementation DBManager
{
    FMDatabase *_dataBase;
    NSLock *_lock;
}

static DBManager *manager = nil;
+(DBManager *)shareManager{
    @synchronized(self){
        if (manager == nil) {
            manager = [[DBManager alloc] init];
        }
    }
    return manager;
}

- (id)init{
    self = [super init];
    if (self) {
       _lock = [[NSLock alloc] init];

        NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/order.db"];
        //初始化
        _dataBase = [[FMDatabase alloc] initWithPath:path];
        //open (第一次创建并打开user.db，之后user.db已经存在，在执行open，会直接打开user.db)
        if ([_dataBase open]) {
            //创建并打开成功
            NSString *createSql = @"create table if not exists orderInfo(id integer primary key autoincrement,orderStr varchar(256),orderTime varchar(256),userID varchar(256),payNum varchar(256))";
            //executeUpdate: 参数为写好的sql语句(创建表，增，删，改，表中的数据 全部调用executeUpdate +sql)
            //返回值为此次操作是否成功
           BOOL isSuccessed = [_dataBase executeUpdate:createSql];
            if (isSuccessed== NO) {
                //lastErrorMessage 获取最近一次的错误信息
                NSLog(@"createError:%@",_dataBase.lastErrorMessage);
            }
            
        }else{
            NSLog(@"create and open error!");
        }
    }
    return self;
}

//插入数据
- (void)insertData:(OrderModel *)model{

    [_lock lock];

    NSString *insertSql = @"insert into orderInfo(orderStr,orderTime,userID,payNum) values(?,?,?,?)";
    //用FMDB执行此sql语句，并传入参数(executeUpdate：后面跟的参数必须为NSObject*类型的(NSString,NSNumber等))
   BOOL isSuccessed =[_dataBase executeUpdate:insertSql,model.orderStr,model.orderTime,model.userID,model.payNum];
    if (!isSuccessed) {
        NSLog(@"insertError:%@",_dataBase.lastErrorMessage);
    }
    [_lock unlock];
}


//删除
- (void)deleteDataWithOrderStr:(NSString *)orderStr{
   NSString *deleteSql = @"delete from userInfo where orderStr = (?)";
   BOOL isSuccessed =[_dataBase executeUpdate:deleteSql,orderStr];
    if (!isSuccessed) {
        NSLog(@"delete error:%@",_dataBase.lastErrorMessage);
    }
}
//修改 (根据model中的值和userId,修改某条数据)
//- (void)upDateWithModel:(UserModel *)model userId:(NSInteger)userId{
//    NSData *data = UIImagePNGRepresentation(model.headImage);
//    //修改的sql语句
//    NSString *updateSql = @"update userInfo set name = ?, age=?, image = ? where id = ?";
//    NSNumber *objId = [NSNumber numberWithInteger:userId];
//    BOOL isSuccessed = [_dataBase executeUpdate:updateSql,model.userName,model.age,data,objId];
//    if (!isSuccessed) {
//        NSLog(@"update error:%@",_dataBase.lastErrorMessage);
//    }
//}
////取到所有的数据
//- (NSArray*)fetchAllUsers{
//    [_lock lock];
//    NSString *selectSql = @"select * from userInfo";
//    //查询的sql语句用executeQuery 方法 ,会将查询的所有结果放入FMResultSet的对象中返回
//    //FMResultSet 是开源库中存放查询结果的类
//    FMResultSet *set = [_dataBase executeQuery:selectSql];
//    //next 方法，会将数据从第一条逐个取出，并会判断是否还有下一条数据，有下一条，返回值为YES，没有则为NO
//    NSMutableArray *array = [NSMutableArray array];
//    while ([set next]) {
//       //stringForColumn： 根据字段名称，将字段所对应的字符串的值取出
//       NSString *userName = [set stringForColumn:@"name"];
//       NSString *age = [set  stringForColumn:@"age"];
//        //dataForColumn 取到的为NSData的数据
//       NSData *imageData = [set dataForColumn:@"image"];
//        UserModel *model = [[UserModel alloc] init];
//        model.userName = userName;
//        model.age = age;
//        model.headImage = [UIImage imageWithData:imageData];
//        [array addObject:model];
//        [model release];
//    }
//    [_lock unlock];
//    return array;
//}


@end
